package com.jdew.thkj.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.HistorySearchAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.ListDataSave;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.onitemInterface.OnItemDelectListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yangyang on 2017/9/13.
 */

public class MainSearchActivity extends BaseActivity {
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.recycleview)
    RecyclerView recycleview;
    @Bind(R.id.dayi)
    TextView dayi;
    @Bind(R.id.kuzi)
    TextView kuzi;
    @Bind(R.id.waitao)
    TextView waitao;
    @Bind(R.id.xiezi)
    TextView xiezi;
    @Bind(R.id.chenshan)
    TextView chenshan;
    @Bind(R.id.clear)
    ImageView clear;
    private List<String> historyList = new ArrayList<>();
    private List<String> hotList = new ArrayList<>();
    private HistorySearchAdapter adapter;
    private List<String> list = new ArrayList<>();
    private ListDataSave listsave;
    private String source;


    @Override
    public int bindLayout() {
        return R.layout.activity_main_search;
    }

    @Override
    public void initView(View view) {
        hotList.add("大衣");
        hotList.add("裤子");
        hotList.add("外套");
        hotList.add("鞋子");
        hotList.add("衬衫");
        showSoftInputFromWindow(search);
        source = getIntent().getStringExtra("source");
        dayi.setOnClickListener(this);
        kuzi.setOnClickListener(this);
        waitao.setOnClickListener(this);
        xiezi.setOnClickListener(this);
        chenshan.setOnClickListener(this);
        adapter = new HistorySearchAdapter(this, list);
        view.findViewById(R.id.view).setVisibility(View.VISIBLE);
        view.findViewById(R.id.back).setOnClickListener(this);
        right.setOnClickListener(this);
        clear.setOnClickListener(this);
        recycleview.setLayoutManager(new LinearLayoutManager(this));
        recycleview.setAdapter(adapter);
        adapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                search.setText(historyList.get(position));
                search.setSelection(search.getText().toString().length());
            }
        });
        adapter.setDelectItemClickListener(new OnItemDelectListener() {
            @Override
            public void onDelectItemClick(int position) {
                if (historyList.size() == 1) {
                    list.clear();
                    historyList.clear();
                    adapter.setList(list);
                    listsave.clear();
                } else {
                    String value = historyList.get(position);
                    historyList.remove(value);
                    list.clear();
                    list.addAll(historyList);
                    listsave.setDataList("history_record", list);
                    adapter.setList(list);
                }
            }
        });
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.right: {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if ("".equals(search.getText().toString().replaceAll(" ", ""))) {
                            return;
                        }
                        if (historyList.size() < 10) {
                            if (!historyList.contains(search.getText().toString())) {
                                historyList.add(search.getText().toString());
                                list.add(search.getText().toString());
                                listsave.setDataList("history_record", list);
                            }
                        } else {
                            if (!historyList.contains(search.getText().toString())) {
                                historyList.remove(0);
                                list.remove(0);
                                historyList.add(search.getText().toString());
                                list.add(search.getText().toString());
                                listsave.setDataList("history_record", list);
                            }
                        }
                        Intent intent;
                        if (null != source && source.equals("pdd")) {
                            intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                        } else if (null != source && source.equals("jd")) {
                            intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                        } else {
                            intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                        }
                        intent.putExtra("result", search.getText().toString());
                        hideWriting(right);
                        startActivity(intent);
                    }
                }).start();
            }
            break;
            case R.id.clear: {
                list.clear();
                historyList.clear();
                adapter.setList(list);
                listsave.clear();
            }
            break;
            case R.id.dayi: {
                search.setText("大衣");
                search.setSelection(hotList.get(0).length());
                Intent intent;
                if (null != source && source.equals("pdd")) {
                    intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                } else if (null != source && source.equals("jd")) {
                    intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                } else {
                    intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                }
                intent.putExtra("result", hotList.get(0));
                setResult(1, intent);
                hideWriting(right);
                startActivity(intent);
            }
            break;
            case R.id.kuzi: {
                search.setText("裤子");
                search.setSelection(hotList.get(1).length());
                Intent intent;
                if (null != source && source.equals("pdd")) {
                    intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                } else if(null != source && source.equals("jd")){
                    intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                }else{
                    intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                }
                intent.putExtra("result", hotList.get(1));
                setResult(1, intent);
                hideWriting(right);
                startActivity(intent);
            }
            break;
            case R.id.waitao: {
                search.setText("外套");
                search.setSelection(hotList.get(2).length());
                Intent intent;
                if (null != source && source.equals("pdd")) {
                    intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                } else if(null != source && source.equals("jd")){
                    intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                }else{
                    intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                }
                intent.putExtra("result", hotList.get(2));
                setResult(1, intent);
                hideWriting(right);
                startActivity(intent);
            }
            break;
            case R.id.xiezi: {
                search.setText("鞋子");
                search.setSelection(hotList.get(3).length());
                Intent intent;
                if (null != source && source.equals("pdd")) {
                    intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                } else if(null != source && source.equals("jd")){
                    intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                }else{
                    intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                }
                intent.putExtra("result", hotList.get(3));
                setResult(1, intent);
                hideWriting(right);
                startActivity(intent);
            }
            break;
            case R.id.chenshan: {
                search.setText("衬衫");
                search.setSelection(hotList.get(4).length());
                Intent intent;
                if (null != source && source.equals("pdd")) {
                    intent = new Intent(MainSearchActivity.this, PddSearchActivity.class);
                } else if(null != source && source.equals("jd")){
                    intent = new Intent(MainSearchActivity.this, JDSearchActivity.class);
                }else{
                    intent = new Intent(MainSearchActivity.this, SearchActivity.class);
                }
                intent.putExtra("result", hotList.get(4));
                setResult(1, intent);
                hideWriting(right);
                startActivity(intent);
            }
            break;
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }

    }


    public void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        search.setText("");
        listsave = new ListDataSave(this, "history_record");
        historyList = listsave.getDataList();
        list.clear();
        list.addAll(historyList);
        Collections.reverse(historyList);
        adapter.setList(historyList);
    }

}
